﻿/*
 * This file is part of MonoSettlers.
 *
 * Copyright (C) 2010-2011 Christoph Husse
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: 
 *      # Christoph Husse
 * 
 * Also checkout our homepage: http://opensettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace MonoSettlers
{
    public partial class MovableManager
    {
        public WalkResult EnumFreeSpacesAround(Point inAround, Int32 inWidth, Int32 inHeight, Func<Point, WalkResult> inHandler)
        {
            return GridSearch.GridWalkAround(inAround, Size - inWidth, Size - inHeight, (pos) =>
            {
                for (int x = 0; x < inWidth; x++)
                {
                    for (int y = 0; y < inHeight; y++)
                    {
                        var cell = Terrain[pos.X + x, pos.Y + y];

                        if (cell.WallValue != 0)
                            return WalkResult.NotFound;
                    }
                }

                return inHandler(pos);
            });
        }

        public bool EnumTerrainAt(Point inLeftTop, Int32 inWidth, Int32 inHeight, Func<TerrainCell, bool> inHandler)
        {
            bool hasEnumerated = false;

            for (int x = inLeftTop.X; x < Math.Min(Size - 1, inLeftTop.X + inWidth); x++)
            {
                for (int y = inLeftTop.Y; y < Math.Min(Size - 1, inLeftTop.Y + inHeight); y++)
                {
                    hasEnumerated = true;

                    if (!inHandler(Terrain[x, y]))
                        return true;
                }
            }

            return hasEnumerated;
        }
    }
}
